<?php

use Biz\model\NewProduct\NewProductWikiModel;
use Biz\model\NewProduct\NewProductWinnerListModel;
use Doraemon\tools\ImgUrl;

/**
 * Description: 获取我的新品列表
 * Created by PhpStorm.
 * User: 玉坤
 * Date: 2021/6/17
 * Time: 11:04
 */
class GetMyLotteryListAction extends ActionPlugin
{
    public const USER_LEVEL = self::LEVEL_REGISTER;

    public function main()
    {
        $uid = $this->uid;
        if (!$uid) {
            throw new \Exception("请登录重试！");
        }
        $where['uid'] = $uid;
        $list = \Biz\model\NewProduct\NewProductWinnerListModel::all($where, '*');

        if ($list) {
            $nids = array_column($list, 'nid');

            $new_pro_list = \Biz\service\NewProduct\NewProductService::getNewProByIds($nids,
                ['brand_id', 'price', 'id', 'img']);


            $brand_ids = array_column($new_pro_list, 'brand_id');
            $brand_list = \Biz\service\brand\Brand::getBrandInfoByIds4Goods($brand_ids);

            foreach ($new_pro_list as &$item) {
                $item['brand_info'] = $brand_list[$item['brand_id']];
            }
            $new_pro_list = array_column($new_pro_list, null, 'id');
            $winner_status_name = NewProductWinnerListModel::WINNER_STATUS;
            foreach ($list as &$v) {
                if ((int)$v['type'] === NewProductWikiModel::GRACE_PDT) {
                    $v['price'] = 50;
                    $v['img'] = NewProductWinnerListModel::JD_IMG;
                    $v['brand_info'] = null;
                    $v['winner_status_name'] = $winner_status_name[$v['winner_status']];
                    $v['jd_link'] = "https://item.jd.com";
                } else {
                    $v['img'] = ImgUrl::getUrl(ImgUrl::APPTYPE_BIZ, $new_pro_list[$v['nid']]["img"],
                        ImgUrl::IMAGE_SIZE_SQ_PHONE);
                    $v['price'] = $new_pro_list[$v['nid']]['price'] ? bcdiv($new_pro_list[$v['nid']]['price'], 100,
                        2) : "0.00";
                    $v['brand_info'] = $new_pro_list[$v['nid']]['brand_info'];
                    $v['winner_status_name'] = $winner_status_name[$v['winner_status']];
                }


            }
        }
        $list = $list ?? [];
        $this->setSuccess($list);
    }

}